import i18n from "@/language";

export function max(list) {
  return Math.max.apply(null, list.filter(item => !isNaN(item)))
}

export function map(list, fn) {
  return Array.prototype.map.call(list, fn)
}

export function isEmpty(item) {
  return item.length === 0
}

/**
 * @description 计算字符串的长度
 * @param {*} str 
 * @returns 
 */
export function calStringWidth(string, propsData) {
  // console.log(`propsData ==>`, propsData);
  const { label, sortable, customSort } = propsData
  /**
   * 是否是操作项
   */
  const isOption = label === i18n.t('操作')
  let cnWidth = 20
  const enWidth = 11
  const jpWidth = 18
  const otherWidth = 10
  const minColoumnWidth = 70
  const regexCn = /[\u4e00-\u9fa5]/
  const regexEn = /^[A-Za-z]+$/
  const regexJp = /[\u0800-\u4e00]/
  let obj = {
    cn: 0,
    en: 0,
    jp: 0,
    other: 0,
  }
  for (const str of string) {
    // 中文
    if (regexCn.test(str)) {
      obj.cn += 1
    } else if (regexJp.test(str)) {
      // 日文
      obj.jp += 1
    } else if (regexEn.test(str)) {
      // 英文
      obj.en += 1
    } else {
      // 其他
      obj.other += 1
    }

  }

  const { cn, en, jp, other } = obj
  if (cn > 4) {
    cnWidth = 18
  }

  let targetWidth = cn * cnWidth + en * enWidth + jp * jpWidth + other * otherWidth
  // console.log(`string ==>`, string);
  // console.log(`obj ==>`, obj);
  // console.log(`targetWidth ==>`, targetWidth);
  if (sortable || sortable == '' || customSort || customSort == '') {/**是否显示排序 */
    targetWidth += 40
  }

  if (cn > 7) {
    targetWidth = targetWidth / 2
  }
  // console.log(string,targetWidth > minColoumnWidth ? targetWidth : minColoumnWidth);
  // console.log(`targetWidth ==>`, obj, label, targetWidth);

  return targetWidth > minColoumnWidth ? targetWidth : minColoumnWidth
}
